home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT16.ZIP / TUTPRO16.PAS < prev   
Pascal/Delphi Source File  |  1994-09-23  |  6KB  |  196 lines

  1. {$X+}
  2. USES crt,gfx2;
  3.  
  4. Type Pallette = Array [0..255,1..3] of byte;
  5.  
  6. VAR virscr2:virtptr;
  7.     vaddr2:word;
  8.  
  9. {──────────────────────────────────────────────────────────────────────────}
  10. Procedure LoadCELPal (FileName : String; Var Palette : Pallette);
  11.   { This loads in the pallette of the .CEL file into the variable Palette }
  12. Var
  13.   Fil : file;
  14. Begin
  15.   Assign (Fil, FileName);
  16.   Reset (Fil, 1);
  17.   Seek(Fil,32);
  18.   BlockRead (Fil, Palette, 768);
  19.   Close (Fil);
  20. End;
  21.  
  22.  
  23. {──────────────────────────────────────────────────────────────────────────}
  24. Procedure Init;
  25. VAR loop1,loop2:integer;
  26.     tpal:pallette;
  27. BEGIN
  28.   getmem (virscr2,sizeof(virscr2^));
  29.   vaddr2:=seg(virscr2^);
  30.   cls (vaddr2,0);
  31.   cls (vaddr,0);
  32.   loadcelpal ('to.cel',tpal);
  33.   for loop1:=0 to 255 do
  34.     pal (loop1,tpal[loop1,1],tpal[loop1,2],tpal[loop1,3]);
  35.   loadcel ('to.cel',virscr);
  36.   for loop1:=0 to 319 do
  37.     for loop2:=0 to 199 do
  38.       if getpixel (loop1,loop2,vaddr)=0 then
  39.         putpixel (loop1,loop2,(loop1+loop2) mod 256,vaddr);
  40. END;
  41.  
  42. {──────────────────────────────────────────────────────────────────────────}
  43. Procedure Scale (x,y,w,h,origw,origh,source,dest:word); assembler;
  44.   { This scales the picture to the size of w and h, and places the result
  45.     at x , y. Origw and origh are the origional width and height of the
  46.     bitmap. The bitmap must start at the beginning of a segment, with
  47.     source being the segment value. The image is placed in screen at dest}
  48. VAR jx,jy,depth,temp:word;
  49. asm
  50.   push  ds
  51.  
  52.   mov   ax,source
  53.   mov   ds,ax
  54.   mov   ax,dest
  55.   mov   es,ax
  56.   mov   depth,0
  57.   dec   h
  58.  
  59.   xor   dx,dx
  60.   mov   ax,origw
  61.   shl   ax,6
  62.   mov   bx,w
  63.   div   bx
  64.   shl   ax,2
  65.   mov   jx,ax     { jx:=origw*256/w }
  66.  
  67.   xor   dx,dx
  68.   mov   ax,origh
  69.   shl   ax,6
  70.   mov   bx,h
  71.   div   bx
  72.   shl   ax,2
  73.   mov   jy,ax     { jy:=origh*256/h }
  74.  
  75.   xor   cx,cx
  76. @Loop2 :          { vertical loop }
  77.   push  cx
  78.   mov   ax,depth
  79.   add   ax,jy
  80.   mov   depth,ax
  81.  
  82.   xor   dx,dx
  83.   mov   ax,depth
  84.   shr   ax,8
  85.   mov   bx,origw
  86.   mul   bx
  87.   mov   temp,ax   { temp:=depth shr 8*origw;}
  88.  
  89.  
  90.   mov   di,y
  91.   add   di,cx
  92.   mov   bx,di
  93.   shl   di,8
  94.   shl   bx,6
  95.   add   di,bx
  96.   add   di,x      { es:di = dest ... di=(loop1+y)*320+x }
  97.  
  98.   mov   cx,w
  99.   xor   bx,bx
  100.   mov   dx,jx
  101.   mov   ax,temp
  102. @Loop1 :          { horizontal loop }
  103.   mov   si,bx
  104.   shr   si,8
  105.   add   si,ax     { ax = temp = start of line }
  106.  
  107.   movsb           { si=temp+(si shr 8) }
  108.   add   bx,dx
  109.  
  110.   dec   cx
  111.   jnz   @loop1    { horizontal loop }
  112.  
  113.   pop   cx
  114.   inc   cx
  115.   cmp   cx,h
  116.   jl    @loop2    { vertical loop }
  117.  
  118.   pop   ds
  119. end;
  120.  
  121. {──────────────────────────────────────────────────────────────────────────}
  122. Procedure Play;
  123. VAR x,y,z,loop1:integer;
  124. BEGIN
  125.   z:=114;
  126.   while keypressed do readkey;
  127.   Repeat
  128.     for loop1:=1 to 50 do BEGIN
  129.       dec (z,2);
  130.       x:=16 shl 8 div z;
  131.       y:=10 shl 8 div z; { Perspective transforms ... makes the zoom smoother }
  132.       cls (vaddr2,0);
  133.       scale (160-(x shr 1),100-(y shr 1),x,y,320,200,vaddr,vaddr2);
  134.       flip (vaddr2,vga);
  135.     END;   { Scale towards you }
  136.     for loop1:=1 to 50 do BEGIN
  137.       inc (z,2);
  138.       x:=16 shl 8 div z;
  139.       y:=10 shl 8 div z; { Perspective transforms ... makes the zoom smoother }
  140.       cls (vaddr2,0);
  141.       scale (160-(x shr 1),100-(y shr 1),x,y,320,200,vaddr,vaddr2);
  142.       flip (vaddr2,vga);
  143.     END;   { Scale away from you }
  144.   Until keypressed;
  145.   while keypressed do readkey;
  146. END;
  147.  
  148. BEGIN
  149.   clrscr;
  150.   writeln ('Hokay! Here is the sixteenth tutorial! This one is on nice fast 2d');
  151.   writeln ('scaling, for any size bitmap. Just hit any key and it will scale a');
  152.   writeln ('picture up and down. Clipping is NOT performed, so the destination');
  153.   writeln ('pic MUST fit in the screen boundaries. In one zoom towards and away');
  154.   writeln ('from you there is 100 frames.');
  155.   writeln;
  156.   Writeln ('You can make many nice effects with scaling, this "bouncing" is just');
  157.   writeln ('one of them ... go on, amaze everyone with your ingenuity ;-) Also,');
  158.   writeln ('why not test your coding mettle, so to speak, by implementing clipping?');
  159.   Writeln;
  160.   writeln ('The routine could greatly be speeded up with 386 extended registers, but');
  161.   writeln ('for the sake of compatability I have kept it to 286 code. Also, this');
  162.   writeln ('routine isn''t fully optimised .. you may be able to get some speedups');
  163.   writeln ('out of it... (probably by moving the finding of DI out of the loop and');
  164.   writeln ('just adding a constant for each line ... hint hint) ;)');
  165.   writeln;
  166.   writeln ('The pic was drawn by me for Tut11, I am reusing it because I am at varsity..');
  167.   writeln ('without a mouse. :(');
  168.   writeln;
  169.   writeln;
  170.   writeln ('Hit any key to continue ... ');
  171.   readkey;
  172.   setupvirtual;
  173.   setmcga;
  174.   init;
  175.   play;
  176.   settext;
  177.   shutdown;
  178.   freemem (virscr2,sizeof(virscr2^));
  179.   Writeln ('All done. This concludes the sixteenth sample program in the ASPHYXIA');
  180.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  181.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS.I also occasinally');
  182.   Writeln ('RSAProg, comp.lang.pascal and comp.sys.ibm.pc.demos. E-mail me at :');
  183.   Writeln ('    denthor@beastie.cs.und.ac.za');
  184.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  185.   Writeln ('             Grant Smith');
  186.   Writeln ('             P.O. Box 270');
  187.   Writeln ('             Kloof');
  188.   Writeln ('             3640');
  189.   Writeln ('             Natal');
  190.   Writeln ('             South Africa');
  191.   Writeln ('I hope to hear from you soon!');
  192.   Writeln; Writeln;
  193.   Write   ('Hit any key to exit ...');
  194.   readkey;
  195. END.
  196.